package edu.ybu.manage.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.ybu.manage.pojo.Entor;
import edu.ybu.manage.pojo.Gao;
import edu.ybu.manage.service.EntorService;
import edu.ybu.manage.service.GaoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/student")
public class GaoController {
    @Autowired
    private GaoService gaoService;


    @GetMapping("/selectAllc")
    public Map<String,Object> selectAll(){
        List<Gao> gaos = gaoService.list();
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("code", 0);
        res.put("msg", "");
        res.put("count", gaos.size());
        res.put("data",gaos );
        return  res;
    }


    /**
     * 条件查询

     * @return
     */
    @GetMapping("/selectByConditionc")
    public Map<String,Object> selectByCondition(String studentnamec){


//    设置条件查询
        LambdaQueryWrapper<Gao> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(Gao::getId);
        queryWrapper.like(Gao::getStudentnamec,studentnamec);
        List<Gao> gaos = gaoService.list(queryWrapper);
        Map<String, Object> res = new HashMap<String, Object>();
        res.put("code", 0);
        res.put("msg", "");
        res.put("count", gaos.size());
        res.put("data",gaos );
        return  res;

    }

    /**
     * 增加信息

     * @return
     */
    @GetMapping("/insertStudentc")
    public Integer insertStudent(String json){
        ObjectMapper objectMapper = new ObjectMapper();
        Gao gao = null;
        try {
            gao= objectMapper.readValue(json,Gao.class);
        } catch (JsonProcessingException e) {
            System.out.println("json转换失败");
        }
        if (gao==null){
            return 0;
        }
        return gaoService.insert(gao);
    }




    /**
     * 删除功能
     * @param
     * @return
     */
    @GetMapping("/deleteStudentc")
    public Integer delectUsers(String json){
        ObjectMapper objectMapper = new ObjectMapper();
        Gao gao = null;
        try {
            gao= objectMapper.readValue(json,Gao.class);
        } catch (JsonProcessingException e) {
            System.out.println("json转换失败");
        }
        if (gao==null){
            return 0;
        }
        return gaoService.delect(gao.getId());

    }

    /**
     * 编辑用户
     * @return
     */
    @GetMapping("/updateByIdc")
    public Integer updateById(String json){
        ObjectMapper objectMapper = new ObjectMapper();
        Gao gao = null;
        try {
            gao=objectMapper.readValue(json,Gao.class);
        } catch (JsonProcessingException e) {
            System.out.println("json转换失败");
        }
        return gaoService.update(gao);
    }
}
